using System;
using System.Diagnostics;
using NUnit.Framework;

using BuildAutoIncrement;

namespace TestUnits {
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    [TestFixture]
    public class ProjectVersionTest {

        private class TestableProjectVersion : ProjectVersion {
            public TestableProjectVersion(string version) : base(version) {
            }
        }
	
        public ProjectVersionTest() {
        }

        [SetUp] 
        public void SetUp() {
        }

        [TearDown] 
        public void TearDown() {
        }

        
        public void TestCreation() {
            ProjectVersion empty = ProjectVersion.Empty;
            Assert.IsTrue(empty != null);
            ProjectVersion min = ProjectVersion.MinValue;
            Assert.IsTrue(min != null);

            ProjectVersion p1 = new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion);
            Assert.IsTrue(p1 != null);
            p1 = new ProjectVersion("1.2.*", AssemblyVersionType.AssemblyVersion);
            Assert.IsTrue(p1 != null);
            p1 = new ProjectVersion("1.2.3.*", AssemblyVersionType.AssemblyVersion);
            Assert.IsTrue(p1 != null);
            p1 = new ProjectVersion("1.2.3.4", AssemblyVersionType.AssemblyVersion);
            Assert.IsTrue(p1 != null);
            p1 = new ProjectVersion("a.b.c.d", AssemblyVersionType.AssemblyVersion);
            Assert.IsTrue(p1 == ProjectVersion.Invalid);
        }


        public void TestToString() {
            ProjectVersion p1 = new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion);
            Assert.IsTrue(p1.ToString() == "1.2");

            p1 = new ProjectVersion("1.2.*", AssemblyVersionType.AssemblyVersion);
            Assert.IsTrue(p1.ToString() == "1.2.*");

            p1 = new ProjectVersion("1.2.3.*", AssemblyVersionType.AssemblyVersion);
            Assert.IsTrue(p1.ToString() == "1.2.3.*");

            p1 = new ProjectVersion("1.2.3.4", AssemblyVersionType.AssemblyVersion);
            Assert.IsTrue(p1.ToString() == "1.2.3.4");

            p1 = ProjectVersion.Empty;
            Assert.IsTrue(p1.ToString() == "");

            p1 = ProjectVersion.Invalid;
            Assert.IsTrue(p1.ToString() == "");

            p1 = new ProjectVersion("a.b.c.d", AssemblyVersionType.AssemblyVersion);
            Assert.IsTrue(p1.ToString() == "a.b.c.d");
        }

        
        public void TestComparison() {
            ProjectVersion p1 = new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion);
            ProjectVersion p2 = new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion);
            Assert.IsTrue(p1.Equals(p2));
            Assert.IsTrue(p1 == p2);
            Assert.IsFalse(p1 != p2);
            Assert.IsTrue(p1 >= p2);
            Assert.IsTrue(p1 <= p2);
            Assert.IsFalse(p1 > p2);
            Assert.IsFalse(p1 < p2);

            p1 = new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion);
            p2 = new ProjectVersion("1.2.0", AssemblyVersionType.AssemblyVersion);
            Assert.IsTrue(p1.Equals(p2));
            Assert.IsTrue(p1 == p2);
            Assert.IsFalse(p1 != p2);
            Assert.IsTrue(p1 >= p2);
            Assert.IsTrue(p1 <= p2);
            Assert.IsFalse(p1 > p2);
            Assert.IsFalse(p1 < p2);

            p1 = new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion);
            p2 = new ProjectVersion("1.2.0.0", AssemblyVersionType.AssemblyVersion);
            Assert.IsTrue(p1.Equals(p2));
            Assert.IsTrue(p1 == p2);
            Assert.IsFalse(p1 != p2);
            Assert.IsTrue(p1 >= p2);
            Assert.IsTrue(p1 <= p2);
            Assert.IsFalse(p1 > p2);
            Assert.IsFalse(p1 < p2);

            p1 = new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion);
            p2 = new ProjectVersion("1.3", AssemblyVersionType.AssemblyVersion);
            Assert.IsFalse(p1.Equals(p2));
            Assert.IsTrue(p1 != p2);
            Assert.IsFalse(p1 == p2);
            Assert.IsTrue(p1 <= p2);
            Assert.IsFalse(p1 >= p2);
            Assert.IsFalse(p1 > p2);
            Assert.IsTrue(p1 < p2);

            p1 = new ProjectVersion("1.3", AssemblyVersionType.AssemblyVersion);
            p2 = new ProjectVersion("1.3.*", AssemblyVersionType.AssemblyVersion);
            Assert.IsFalse(p1.Equals(p2));
            Assert.IsTrue(p1 != p2);
            Assert.IsFalse(p1 == p2);
            Assert.IsTrue(p1 <= p2);
            Assert.IsFalse(p1 >= p2);
            Assert.IsFalse(p1 > p2);
            Assert.IsTrue(p1 < p2);

            p1 = new ProjectVersion("1.3.*", AssemblyVersionType.AssemblyVersion);
            p2 = new ProjectVersion("1.3.*", AssemblyVersionType.AssemblyVersion);
            Assert.IsTrue(p1.Equals(p2));
            Assert.IsFalse(p1 != p2);
            Assert.IsTrue(p1 == p2);
            Assert.IsTrue(p1 <= p2);
            Assert.IsTrue(p1 >= p2);
            Assert.IsFalse(p1 > p2);
            Assert.IsFalse(p1 < p2);
 
            p1 = new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion);
            p2 = new ProjectVersion("1.2.1", AssemblyVersionType.AssemblyVersion);
            Assert.IsFalse(p1.Equals(p2));
            Assert.IsFalse(p1 == p2);
            Assert.IsTrue(p1 != p2);
            Assert.IsFalse(p1 >= p2);
            Assert.IsTrue(p1 <= p2);
            Assert.IsFalse(p1 > p2);
            Assert.IsTrue(p1 < p2);
        
            p1 = new ProjectVersion("1.2.1.0", AssemblyVersionType.AssemblyVersion);
            p2 = new ProjectVersion("1.2.1", AssemblyVersionType.AssemblyVersion);
            Assert.IsTrue(p1.Equals(p2));
            Assert.IsTrue(p1 == p2);
            Assert.IsFalse(p1 != p2);
            Assert.IsTrue(p1 >= p2);
            Assert.IsTrue(p1 <= p2);
            Assert.IsFalse(p1 > p2);
            Assert.IsFalse(p1 < p2);
        
            p1 = new ProjectVersion("1.2.1.0", AssemblyVersionType.AssemblyVersion);
            p2 = new ProjectVersion("1.2.1.*", AssemblyVersionType.AssemblyVersion);
            Assert.IsFalse(p1.Equals(p2));
            Assert.IsFalse(p1 == p2);
            Assert.IsTrue(p1 != p2);
            Assert.IsFalse(p1 >= p2);
            Assert.IsTrue(p1 <= p2);
            Assert.IsFalse(p1 > p2);
            Assert.IsTrue(p1 < p2);
        
            p1 = new ProjectVersion("1.2.1.0", AssemblyVersionType.AssemblyVersion);
            p2 = new ProjectVersion("1.2.1.12", AssemblyVersionType.AssemblyVersion);
            Assert.IsFalse(p1.Equals(p2));
            Assert.IsFalse(p1 == p2);
            Assert.IsTrue(p1 != p2);
            Assert.IsFalse(p1 >= p2);
            Assert.IsTrue(p1 <= p2);
            Assert.IsFalse(p1 > p2);
            Assert.IsTrue(p1 < p2);
        
            p1 = new ProjectVersion("1.2.1." + (ProjectVersion.MaxVersion - 1).ToString(), AssemblyVersionType.AssemblyVersion);
            p2 = new ProjectVersion("1.2.1.*", AssemblyVersionType.AssemblyVersion);
            Assert.IsFalse(p1.Equals(p2));
            Assert.IsFalse(p1 == p2);
            Assert.IsTrue(p1 != p2);
            Assert.IsFalse(p1 >= p2);
            Assert.IsTrue(p1 <= p2);
            Assert.IsFalse(p1 > p2);
            Assert.IsTrue(p1 < p2);

            p1 = new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion);
            p2 = new ProjectVersion("a.b.", AssemblyVersionType.AssemblyVersion);
            Assert.IsFalse(p1.Equals(p2));
            Assert.IsFalse(p1 == p2);
            Assert.IsTrue(p1 != p2);
            Assert.IsTrue(p1 >= p2);
            Assert.IsFalse(p1 <= p2);
            Assert.IsTrue(p1 > p2);
            Assert.IsFalse(p1 < p2);

            p1 = new ProjectVersion("a.b.c", AssemblyVersionType.AssemblyVersion);
            p2 = new ProjectVersion("a.b.", AssemblyVersionType.AssemblyVersion);
            Assert.IsTrue(p1.Equals(p2));
            Assert.IsTrue(p1 == p2);
            Assert.IsFalse(p1 != p2);
            Assert.IsTrue(p1 >= p2);
            Assert.IsTrue(p1 <= p2);
            Assert.IsFalse(p1 > p2);
            Assert.IsFalse(p1 < p2);
        }


        public void TestComparisonEmptyAndMin() {
            ProjectVersion p1 = ProjectVersion.Empty;
            ProjectVersion p2 = ProjectVersion.Empty;
            Assert.IsTrue(p1.Equals(p2));
            Assert.IsTrue(p1 == p2);
            Assert.IsFalse(p1 != p2);
            Assert.IsTrue(p1 >= p2);
            Assert.IsTrue(p1 <= p2);
            Assert.IsFalse(p1 > p2);
            Assert.IsFalse(p1 < p2);

            p1 = ProjectVersion.Empty;
            p2 = ProjectVersion.Invalid;
            Assert.IsTrue(p1.Equals(p2));
            Assert.IsTrue(p1 == p2);
            Assert.IsFalse(p1 != p2);
            Assert.IsTrue(p1 >= p2);
            Assert.IsTrue(p1 <= p2);
            Assert.IsFalse(p1 > p2);
            Assert.IsFalse(p1 < p2);

            p1 = new ProjectVersion("0.0", AssemblyVersionType.AssemblyVersion);
            p2 = ProjectVersion.Empty;
            Assert.IsFalse(p1.Equals(p2));
            Assert.IsFalse(p1 == p2);
            Assert.IsTrue(p1 != p2);
            Assert.IsTrue(p1 >= p2);
            Assert.IsFalse(p1 <= p2);
            Assert.IsTrue(p1 > p2);
            Assert.IsFalse(p1 < p2);

            p1 = new ProjectVersion("0.0", AssemblyVersionType.AssemblyVersion);
            p2 = ProjectVersion.MinValue;
            Assert.IsTrue(p1.Equals(p2));
            Assert.IsTrue(p1 == p2);
            Assert.IsFalse(p1 != p2);
            Assert.IsTrue(p1 >= p2);
            Assert.IsTrue(p1 <= p2);
            Assert.IsFalse(p1 > p2);
            Assert.IsFalse(p1 < p2);

            p1 = new ProjectVersion("0.0.0.0", AssemblyVersionType.AssemblyVersion);
            p2 = ProjectVersion.MinValue;
            Assert.IsTrue(p1.Equals(p2));
            Assert.IsTrue(p1 == p2);
            Assert.IsFalse(p1 != p2);
            Assert.IsTrue(p1 >= p2);
            Assert.IsTrue(p1 <= p2);
            Assert.IsFalse(p1 > p2);
            Assert.IsFalse(p1 < p2);

            p1 = ProjectVersion.MinValue;
            p2 = ProjectVersion.Empty;
            Assert.IsFalse(p1.Equals(p2));
            Assert.IsFalse(p1 == p2);
            Assert.IsTrue(p1 != p2);
            Assert.IsTrue(p1 >= p2);
            Assert.IsFalse(p1 <= p2);
            Assert.IsTrue(p1 > p2);
            Assert.IsFalse(p1 < p2);

            p1 = new ProjectVersion("0.0.0.1", AssemblyVersionType.AssemblyVersion);
            p2 = ProjectVersion.MinValue;
            Assert.IsFalse(p1.Equals(p2));
            Assert.IsFalse(p1 == p2);
            Assert.IsTrue(p1 != p2);
            Assert.IsTrue(p1 >= p2);
            Assert.IsFalse(p1 <= p2);
            Assert.IsTrue(p1 > p2);
            Assert.IsFalse(p1 < p2);
        }

        /// <summary>
        ///   Tests incrementing version by one.
        /// </summary>
        public void TestVersionIncrementByDefaultStep() {
            NumberingOptions no = new NumberingOptions();
            no.ReplaceAsteriskWithVersionComponents = false;
            no.ResetRevisionOnBuildIncrement = false;
            no.ResetRevisionOnMinorIncrement = false;
            no.ResetBuildOnMinorIncrement = false;

            ProjectVersion p1 = new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementMajorVersion;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("2.0", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "2.0");

            p1 = new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementMinorVersion;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.3", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.3");

            p1 = new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementBuild;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.2");

            p1 = new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementRevision;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.2");

            p1 = new ProjectVersion("1.2.*", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementRevision;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.2.*", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.2.*");

            p1 = new ProjectVersion("1.2.*", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementBuild;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.2.*", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.2.*");

            p1 = new ProjectVersion("1.2.*", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementMinorVersion;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.3.*", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.3.*");

            p1 = new ProjectVersion("1.2.3.*", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementRevision;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.2.3.*", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.2.3.*");

            p1 = new ProjectVersion("1.2.3.*", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementBuild;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.2.4.*", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.2.4.*");


            no.ReplaceAsteriskWithVersionComponents = true;

            p1 = new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementMajorVersion;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("2.0", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "2.0");

            p1 = new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementMinorVersion;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.3", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.3");

            p1 = new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementBuild;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.2");

            p1 = new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementRevision;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.2");

            p1 = new ProjectVersion("1.2.*", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementRevision;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.2.0.1", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.2.0.1");

            p1 = new ProjectVersion("1.2.*", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementBuild;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.2.1.0", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.2.1.0");

            p1 = new ProjectVersion("1.2.*", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementMinorVersion;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.3.0.0", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.3.0.0");

            p1 = new ProjectVersion("1.2.3.*", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementRevision;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.2.3.1", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.2.3.1");

            p1 = new ProjectVersion("1.2.3.*", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementBuild;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.2.4.0", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.2.4.0");
        }


        public void TestVersionIncrement() {
            NumberingOptions no = new NumberingOptions();
            no.ReplaceAsteriskWithVersionComponents = false;
            no.ResetRevisionOnBuildIncrement = false;
            no.ResetRevisionOnMinorIncrement = false;
            no.ResetBuildOnMinorIncrement = false;
            no.IncrementBy = 10;

            ProjectVersion p1 = new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementMajorVersion;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("11.0", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "11.0");

            p1 = new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementMinorVersion;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.12", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.12");

            p1 = new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementBuild;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.2");

            p1 = new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementRevision;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.2");

            p1 = new ProjectVersion("1.2.*", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementRevision;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.2.*", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.2.*");

            p1 = new ProjectVersion("1.2.*", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementBuild;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.2.*", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.2.*");

            p1 = new ProjectVersion("1.2.*", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementMinorVersion;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.12.*", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.12.*");

            p1 = new ProjectVersion("1.2.3.*", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementRevision;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.2.3.*", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.2.3.*");

            p1 = new ProjectVersion("1.2.3.*", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementBuild;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.2.13.*", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.2.13.*");


            no.ReplaceAsteriskWithVersionComponents = true;

            p1 = new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementMajorVersion;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("11.0", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "11.0");

            p1 = new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementMinorVersion;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.12", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.12");

            p1 = new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementBuild;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.2");

            p1 = new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementRevision;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.2", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.2");

            p1 = new ProjectVersion("1.2.*", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementRevision;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.2.0.10", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.2.0.10");

            p1 = new ProjectVersion("1.2.*", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementBuild;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.2.10.0", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.2.10.0");

            p1 = new ProjectVersion("1.2.*", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementMinorVersion;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.12.0.0", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.12.0.0");

            p1 = new ProjectVersion("1.2.3.*", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementRevision;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.2.3.10", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.2.3.10");

            p1 = new ProjectVersion("1.2.3.*", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementBuild;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.2.13.0", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.2.13.0");
        }


        public void TestVersionIncrementWithReset() {
            NumberingOptions no = new NumberingOptions();
            no.ReplaceAsteriskWithVersionComponents = false;
            no.ResetRevisionOnBuildIncrement = true;
            no.ResetRevisionOnMinorIncrement = true;
            no.ResetBuildOnMinorIncrement = true;
            no.ResetBuildAndRevisionTo = 0;

            // test with reset to 0
            ProjectVersion p1 = new ProjectVersion("1.2.3.4", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementBuild;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.2.4.0", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.2.4.0");

            p1 = new ProjectVersion("1.2.3.4", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementMinorVersion;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.3.0.0", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.3.0.0");

            no.ResetRevisionOnMinorIncrement = false;

            p1 = new ProjectVersion("1.2.3.4", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementMinorVersion;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.3.0.4", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.3.0.4");

            no.ResetRevisionOnBuildIncrement = false;

            p1 = new ProjectVersion("1.2.3.4", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementBuild;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.2.4.4", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.2.4.4");

            no.ResetBuildOnMinorIncrement = false;

            p1 = new ProjectVersion("1.2.3.4", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementMinorVersion;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.3.3.4", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.3.3.4");

            // test with reset to 1
            no.ReplaceAsteriskWithVersionComponents = false;
            no.ResetRevisionOnBuildIncrement = true;
            no.ResetRevisionOnMinorIncrement = true;
            no.ResetBuildOnMinorIncrement = true;
            no.ResetBuildAndRevisionTo = (ResetBuildAndRevision)1;
        
            p1 = new ProjectVersion("1.2.3.4", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementBuild;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.2.4.1", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.2.4.1");

            p1 = new ProjectVersion("1.2.3.4", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementMinorVersion;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.3.1.1", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.3.1.1");

            no.ResetRevisionOnMinorIncrement = false;

            p1 = new ProjectVersion("1.2.3.4", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementMinorVersion;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.3.1.4", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.3.1.4");

            no.ResetRevisionOnBuildIncrement = false;

            p1 = new ProjectVersion("1.2.3.4", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementBuild;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.2.4.4", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.2.4.4");

            no.ResetBuildOnMinorIncrement = false;

            p1 = new ProjectVersion("1.2.3.4", AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementMinorVersion;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("1.3.3.4", AssemblyVersionType.AssemblyVersion));
            Assert.IsTrue(p1.ToString() == "1.3.3.4");
        }


        public void TestVersionIncrementWithOverflow() {
            NumberingOptions no = new NumberingOptions();

            ProjectVersion p1 = new ProjectVersion("1." + (ProjectVersion.MaxVersion - 1), AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementMinorVersion;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("2.0", AssemblyVersionType.AssemblyVersion));

            p1 = new ProjectVersion("1." + (ProjectVersion.MaxVersion - 1) + "." + (ProjectVersion.MaxVersion - 1) + "." + (ProjectVersion.MaxVersion - 1), AssemblyVersionType.AssemblyVersion);
            no.IncrementScheme = IncrementScheme.IncrementRevision;
            p1.Increment(no);
            Assert.IsTrue(p1 == new ProjectVersion("2.0", AssemblyVersionType.AssemblyVersion), p1.ToString());
        }


        public void TestCompareToPattern() {
            ProjectVersion p1 = new ProjectVersion("1.0", AssemblyVersionType.AssemblyVersion);
            string pattern = "1.0.0.0";
            Assert.IsTrue(p1.CompareToPattern(pattern) == 0);

            p1 = new ProjectVersion("1.0", AssemblyVersionType.AssemblyVersion);
            pattern = "1.1.0.0";
            Assert.IsTrue(p1.CompareToPattern(pattern) < 0);

            p1 = new ProjectVersion("1.1", AssemblyVersionType.AssemblyVersion);
            pattern = "1.0.0.0";
            Assert.IsTrue(p1.CompareToPattern(pattern) > 0);

            p1 = new ProjectVersion("1.1", AssemblyVersionType.AssemblyVersion);
            pattern = "1.0.1.0";
            Assert.IsTrue(p1.CompareToPattern(pattern) > 0);
        
            p1 = new ProjectVersion("1.0", AssemblyVersionType.AssemblyVersion);
            pattern = "1.0.1.0";
            Assert.IsTrue(p1.CompareToPattern(pattern) < 0);
        
            p1 = new ProjectVersion("1.0", AssemblyVersionType.AssemblyVersion);
            pattern = "1.0.*.*";
            Assert.IsTrue(p1.CompareToPattern(pattern) == 0);

            p1 = new ProjectVersion("1.1", AssemblyVersionType.AssemblyVersion);
            pattern = "1.0.*.*";
            Assert.IsTrue(p1.CompareToPattern(pattern) > 0);
        }


        public void TestApplyPattern() {
            string version = "1.2.3.4";
            string pattern = "5.6.7.8";
            string result = ProjectVersion.ApplyVersionPattern(pattern, version, 0);
            Assert.IsTrue(result == pattern);

            version = "1.2.*";
            pattern = "5.6.7.8";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 0);
            Assert.IsTrue(result == pattern);

            version = "1.2";
            pattern = "5.6.7.8";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 0);
            Assert.IsTrue(result == "5.6");

            version = "1.2.3.4";
            pattern = "5.6.*.*";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 0);
            Assert.IsTrue(result == "5.6.3.4");

            version = "1.2.3.4";
            pattern = "5.6.*.8";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 0);
            Assert.IsTrue(result == "5.6.3.8");

            version = "1.2.3.4";
            pattern = "5.*.8.*";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 0);
            Assert.IsTrue(result == "5.2.8.4");

            version = "1.2";
            pattern = "5.6.*.*";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 0);
            Assert.IsTrue(result == "5.6");

            version = "1.2";
            pattern = "5.6.7.*";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 0);
            Assert.IsTrue(result == "5.6");

            version = "1.2";
            pattern = "5.6.*.8";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 0);
            Assert.IsTrue(result == "5.6");

            version = "1.2.*";
            pattern = "5.6.*.*";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 0);
            Assert.IsTrue(result == "5.6.*");

            version = "1.2.*";
            pattern = "5.6.*.8";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 0);
            Assert.IsTrue(result == "5.6.0.8");

            version = "1.*";
            pattern = "5.*.*.8";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 0);
            Assert.IsTrue(result == "5.0.0.8");

            version = "1.2.3.*";
            pattern = "5.6.*.*";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 0);
            Assert.IsTrue(result == "5.6.3.*");

            version = "1.2.3.4";
            pattern = "+.+.+.+";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 0);
            Assert.IsTrue(result == "2.3.4.5");

            version = "1.2.3.4";
            pattern = "+.2.5.+";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 0);
            Assert.IsTrue(result == "2.2.5.5");

            version = "1.2.3.4";
            pattern = "+.*.*.+";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 0);
            Assert.IsTrue(result == "2.2.3.5");

            version = "1.2";
            pattern = "+.+.*.*";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 0);
            Assert.IsTrue(result == "2.3");

            version = "1.2";
            pattern = "+.+.+.+";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 0);
            Assert.IsTrue(result == "2.3");

            version = "1.2.*";
            pattern = "+.+.+.+";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 0);
            Assert.IsTrue(result == "2.3.*");

            version = "1.2.3.4";
            pattern = "+2.+3.+4.+5";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 0);
            Assert.IsTrue(result == "3.5.7.9");

            version = "1.2.3.4";
            pattern = "+2.2.5.+5";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 0);
            Assert.IsTrue(result == "3.2.5.9");

            version = "1.2.3.4";
            pattern = "+2.*.*.+4";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 0);
            Assert.IsTrue(result == "3.2.3.8");

            version = "1.2";
            pattern = "+2.+3.*.*";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 0);
            Assert.IsTrue(result == "3.5");

            version = "1.2";
            pattern = "+2.+3.+4.+5";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 0);
            Assert.IsTrue(result == "3.5");

            version = "1.2.*";
            pattern = "+2.+3.+4.+5";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 0);
            Assert.IsTrue(result == "3.5.*");

            // with reset to 1
            version = "1.2.3.4";
            pattern = "5.6.7.8";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 1);
            Assert.IsTrue(result == pattern);

            version = "1.2.*";
            pattern = "5.6.7.8";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 1);
            Assert.IsTrue(result == pattern);

            version = "1.2";
            pattern = "5.6.7.8";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 1);
            Assert.IsTrue(result == "5.6");

            version = "1.2.3.4";
            pattern = "5.6.*.*";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 1);
            Assert.IsTrue(result == "5.6.3.4");

            version = "1.2.3.4";
            pattern = "5.6.*.8";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 1);
            Assert.IsTrue(result == "5.6.3.8");

            version = "1.2.3.4";
            pattern = "5.*.8.*";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 1);
            Assert.IsTrue(result == "5.2.8.4");

            version = "1.2";
            pattern = "5.6.*.*";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 1);
            Assert.IsTrue(result == "5.6");

            version = "1.2";
            pattern = "5.6.7.*";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 1);
            Assert.IsTrue(result == "5.6");

            version = "1.2";
            pattern = "5.6.*.8";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 1);
            Assert.IsTrue(result == "5.6");

            version = "1.2.*";
            pattern = "5.6.*.*";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 1);
            Assert.IsTrue(result == "5.6.*");

            version = "1.2.*";
            pattern = "5.6.*.8";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 1);
            Assert.IsTrue(result == "5.6.1.8");

            version = "1.*";
            pattern = "5.*.*.8";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 1);
            Assert.IsTrue(result == "5.0.1.8");

            version = "1.2.3.*";
            pattern = "5.6.*.*";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 1);
            Assert.IsTrue(result == "5.6.3.*");

            version = "1.2.3.4";
            pattern = "+.+.+.+";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 1);
            Assert.IsTrue(result == "2.3.4.5");

            version = "1.2.3.4";
            pattern = "+.2.5.+";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 1);
            Assert.IsTrue(result == "2.2.5.5");

            version = "1.2.3.4";
            pattern = "+.*.*.+";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 1);
            Assert.IsTrue(result == "2.2.3.5");

            version = "1.2";
            pattern = "+.+.*.*";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 1);
            Assert.IsTrue(result == "2.3");

            version = "1.2";
            pattern = "+.+.+.+";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 1);
            Assert.IsTrue(result == "2.3");

            version = "1.2.*";
            pattern = "+.+.+.+";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 1);
            Assert.IsTrue(result == "2.3.*");

            version = "1.2.3.4";
            pattern = "+2.+3.+4.+5";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 1);
            Assert.IsTrue(result == "3.5.7.9");

            version = "1.2.3.4";
            pattern = "+2.2.5.+5";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 1);
            Assert.IsTrue(result == "3.2.5.9");

            version = "1.2.3.4";
            pattern = "+2.*.*.+4";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 1);
            Assert.IsTrue(result == "3.2.3.8");

            version = "1.2";
            pattern = "+2.+3.*.*";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 1);
            Assert.IsTrue(result == "3.5");

            version = "1.2";
            pattern = "+2.+3.+4.+5";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 1);
            Assert.IsTrue(result == "3.5");

            version = "1.2.*";
            pattern = "+2.+3.+4.+5";
            result = ProjectVersion.ApplyVersionPattern(pattern, version, 1);
            Assert.IsTrue(result == "3.5.*");
        }


        public void TestValidation() {
            // these are valid version strings
            Assert.IsTrue(ProjectVersion.IsValidVersionString("1.0.0.0", AssemblyVersionType.AssemblyVersion, ProjectType.CSharpProject));
            Assert.IsTrue(ProjectVersion.IsValidVersionString("1.0.0.*", AssemblyVersionType.AssemblyVersion, ProjectType.CSharpProject));
            Assert.IsTrue(ProjectVersion.IsValidVersionString("1.0.0", AssemblyVersionType.AssemblyVersion, ProjectType.CSharpProject));
            Assert.IsTrue(ProjectVersion.IsValidVersionString("1.0.*", AssemblyVersionType.AssemblyVersion, ProjectType.CSharpProject));
            Assert.IsTrue(ProjectVersion.IsValidVersionString("1.0", AssemblyVersionType.AssemblyVersion, ProjectType.CSharpProject));

            // invalid version strings
            Assert.IsFalse(ProjectVersion.IsValidVersionString("", AssemblyVersionType.AssemblyVersion, ProjectType.CSharpProject));
            Assert.IsFalse(ProjectVersion.IsValidVersionString("*", AssemblyVersionType.AssemblyVersion, ProjectType.CSharpProject));
            Assert.IsFalse(ProjectVersion.IsValidVersionString("1", AssemblyVersionType.AssemblyVersion, ProjectType.CSharpProject));
            Assert.IsFalse(ProjectVersion.IsValidVersionString("1.", AssemblyVersionType.AssemblyVersion, ProjectType.CSharpProject));
            Assert.IsFalse(ProjectVersion.IsValidVersionString("1.*", AssemblyVersionType.AssemblyVersion, ProjectType.CSharpProject));
            Assert.IsFalse(ProjectVersion.IsValidVersionString("1.2.*.*", AssemblyVersionType.AssemblyVersion, ProjectType.CSharpProject));
            Assert.IsFalse(ProjectVersion.IsValidVersionString("1.2.*.1", AssemblyVersionType.AssemblyVersion, ProjectType.CSharpProject));
            Assert.IsFalse(ProjectVersion.IsValidVersionString("a", AssemblyVersionType.AssemblyVersion, ProjectType.CSharpProject));
            Assert.IsFalse(ProjectVersion.IsValidVersionString("a.b.c.d", AssemblyVersionType.AssemblyVersion, ProjectType.CSharpProject));
            Assert.IsFalse(ProjectVersion.IsValidVersionString("a.b.*", AssemblyVersionType.AssemblyVersion, ProjectType.CSharpProject));
        }
    }
}